﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HostComputerPlatform.Views
{
    /// <summary>
    /// SystemDialogView.xaml 的交互逻辑
    /// </summary>
    public partial class SystemDialogView : System.Windows.Controls.UserControl
    {
        private System.Windows.Point _lastMousePosition;
        private bool _isMouseCaptured = false;
        public SystemDialogView()
        {
            InitializeComponent();
        }

        private void ScrollViewer_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.MiddleButton == MouseButtonState.Pressed)
            {
                ScrollViewer scrollViewer = sender as ScrollViewer;
                scrollViewer.Cursor = System.Windows.Input.Cursors.Hand;
                _lastMousePosition = e.GetPosition(sender as IInputElement);
                Mouse.Capture(sender as IInputElement);
                _isMouseCaptured = true;
            }
        }

        private void ScrollViewer_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_isMouseCaptured)
            {
                var currentPosition = e.GetPosition(sender as IInputElement);
                var delta = currentPosition - _lastMousePosition;

                // 根据需要调整滚动速度
                var scrollSpeed = 1;
                ScrollViewer scrollViewer = sender as ScrollViewer;
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - delta.Y * scrollSpeed);
                scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset - delta.X * scrollSpeed);

                _lastMousePosition = currentPosition;
            }
        }

        private void ScrollViewer_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (_isMouseCaptured)
            {
                ScrollViewer scrollViewer = sender as ScrollViewer;
                scrollViewer.Cursor = System.Windows.Input.Cursors.Arrow;
                Mouse.Capture(null);
                _isMouseCaptured = false;
            }
        }

        private void MyItemsControl_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            var matrix = MyItemsControl.LayoutTransform.Value;
            if (e.Delta > 0)
            {
                matrix.ScaleAt(1.1, 1.1, e.GetPosition(this).X, e.GetPosition(this).Y);
            }
            else
            {
                matrix.ScaleAt(1.0 / 1.1, 1.0 / 1.1, e.GetPosition(this).X, e.GetPosition(this).Y);
            }
            MyItemsControl.LayoutTransform = new MatrixTransform(matrix);
        }
    }
}
